// subpkg_worker/bloodPressure_week/bloodPressure_week.js
var wxCharts = require('../../utils/wxcharts.js');
var lineChart = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date:'最近一周',
    max_date:new Date().getTime(),
    min_date:new Date(2023,8,1).getTime(),
    show:false,
    startTime:'',
    endTime:'',
    elderlyId:'',
    range:[3,7],
    width:'',
    x_data:[],
    y_data:[],
    elderlyId:'',
    textcolorList:['#014f8e','#bfbfbf','#bfbfbf','#bfbfbf','#bfbfbf','#bfbfbf','#bfbfbf','#bfbfbf','#bfbfbf'],
    backgroundList:['pink','#fff','#fff','#fff','#fff','#fff','#fff','#fff','#fff']
  },

  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirm(event) {
    const [start, end] = event.detail;
    var year = start.getFullYear()
    var month = start.getMonth() + 1 <10?'0'+(start.getMonth() + 1):start.getMonth() + 1
    var day = start.getDate()
    var year1 = end.getFullYear()
    var month1 = end.getMonth() + 1 <10?'0'+(end.getMonth() + 1):end.getMonth() + 1
    var day1 = end.getDate()
    var startTime = `${year}-${month}-${day}T00:00`
    var endTime = `${year1}-${month1}-${day1}T23:59`
    this.setData({
      show: false,
      startTime:startTime,
      endTime:endTime,
      date:`${this.formatDate(start)}-${this.formatDate(end)}`
    });
    this.query('空腹',startTime,endTime)
  },
  query(moment,startTime,endTime){
    wx.request({ 
      url: 'https://zhixiao.site:8080/health/bloodGlucose/query',
      data:{
        elderlyId:this.data.elderlyId,
        moment:moment,
        startTime:startTime,
        endTime:endTime
      },
      success:(res)=>{
        console.log(res)
        if(res.data.data===null){
          this.setData({
            x_data:[],
            y_data:[]
          })
          return
        }
        var bloodGlucose = res.data.data.bloodGlucoses
        var x_data = []
        var y_data = []
        for(var i in bloodGlucose){
          x_data[i] = bloodGlucose[i].createdTime.slice(5,10)+'/'+bloodGlucose[i].createdTime.slice(10,16)
          y_data[i] = bloodGlucose[i].bloodGlucose
        }
        this.setData({
          x_data:x_data,
          y_data:y_data
        })
      },
      complete:(res)=>{
        this.OnWxChart(this.data.x_data,this.data.y_data,moment,this.data.range)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var windowWidth = wx.getSystemInfoSync().windowWidth/750 * 690
    this.setData({
      elderlyId:options.elderId,
      width:windowWidth
    })
    this.setData({
      elderlyId:options.elderId
    })
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth()+1<10?'0'+(date.getMonth()+1):date.getMonth()+1
    var day = date.getDate()
    var endTime = `${year}-${month}-${day}T23:59`
    var date1 = new Date(date.getTime()-6*24*60*60*1000)
    var year1 = date1.getFullYear()
    var month1 = date1.getMonth()+1<10?'0'+(date1.getMonth()+1):date1.getMonth()+1
    var day1 = date1.getDate()
    var startTime = `${year1}-${month1}-${day1}T00:00`
    console.log(startTime)
    console.log(endTime)
    this.setData({
      startTime:startTime,
      endTime:endTime
    })
    this.query('空腹',startTime,endTime)
    // this.getHealth()
    // this.getHealthList()

  },


  changeColor(x){
    const textcolorList = this.data.textcolorList
    const backgroundList = this.data.backgroundList
    for(var i = 0;i<textcolorList.length;i++){
      if(i===x){
        textcolorList[i]="#014f8e"
        backgroundList[i]="pink"
      }else{
        textcolorList[i]="#bfbfbf"
        backgroundList[i]="#fff"

      }
    }
    this.setData({
      textcolorList:textcolorList,
      backgroundList:backgroundList
    })  
  },

  //更换折线图数据为空腹数据
  canvas1_click:function(){
    this.changeColor(0)
    this.query('空腹')
  },
  
  //更换折线图数据为餐后数据
  canvas2_click:function(){
    this.changeColor(1)
    this.query('早餐后',this.data.startTime,this.data.endTime)

  },

  canvas3_click:function(){
    this.changeColor(2)
    this.query('早餐前',this.data.startTime,this.data.endTime)

  },
  canvas4_click:function(){
    this.changeColor(3)
    this.query('午餐后',this.data.startTime,this.data.endTime)

  },
  canvas5_click:function(){
    this.changeColor(4)
    this.query('午餐前',this.data.startTime,this.data.endTime)

  },
  canvas6_click:function(){
    this.changeColor(5)
    this.query('晚餐后',this.data.startTime,this.data.endTime)

  },
  canvas7_click:function(){
    this.changeColor(6)
    this.query('晚餐前',this.data.startTime,this.data.endTime)

  },
  canvas8_click:function(){
    this.changeColor(7)
    this.query('睡前',this.data.startTime,this.data.endTime)

  },
  canvas9_click:function(){
    this.changeColor(8)
    this.query('凌晨',this.data.startTime,this.data.endTime)

  },

 //图表点击事件
 touchcanvas:function(e){
  lineChart.showToolTip(e, {
    format: function (item, category) {
      return category + ' ' + item.name + ':' + item.data
    }
  });
},
//折线图绘制方法
OnWxChart:function(x_data,y_data,name,range){
  
  var windowWidth = '', windowHeight='';    //定义宽高
  try {
    var res = wx.getSystemInfoSync();    //试图获取屏幕宽高数据
      windowWidth = res.windowWidth / 750 * 690;   //以设计图750为主进行比例算换
      windowHeight = res.windowWidth / 750 * 690    //以设计图750为主进行比例算换
      console.log(windowWidth)
      if(x_data.length>5){
        windowWidth = x_data.length*70
        this.setData({
          width:windowWidth+40
        })
      }
  } catch (e) {
    console.error('getSystemInfoSync failed!');   //如果获取失败
  }
  lineChart = new wxCharts({
    canvasId: 'lineCanvas',     //输入wxml中canvas的id
    type: 'line',     
    categories:x_data,    //模拟的x轴横坐标参数
    animation: true,  //是否开启动画
   
    series: [{
      name: name,
      data: y_data,
      format: function (val, name) {
        return val ;
      }
    }
    ],
    xAxis: {   //是否隐藏x轴分割线
      disableGrid: true,
      fontColor:'black',
      
    },
    yAxis: {      //y轴数据
      title: '',  //标题
      format: function (val) {  //返回数值
        return val.toFixed(2);
      },
      min: range[0],   //最小值
      max: range[1],
      
      gridColor: '#D8D8D8',
      fontColor:'black'
      
    },
    width: windowWidth*1.1,  //图表展示内容宽度
    height: windowHeight,  //图表展示内容高度
    dataLabel: true,  //是否在图表上直接显示数据
    dataPointShape: true, //是否在图标上显示数据点标志
    extra: {
      lineStyle: 'Broken'  //曲线
    },
  });
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})